package org.boulderfoodrescue.webapp.client.pickups;

import java.util.Comparator;

import org.boulderfoodrescue.webapp.client.resources.Resources;
import org.boulderfoodrescue.webapp.shared.Location;
import org.boulderfoodrescue.webapp.shared.Pickup;

import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.view.client.ListDataProvider;

public class PickupTable extends FlowPanel{
	protected CellTable<Pickup> table;
	protected ListDataProvider<Pickup> dataProvider;
	public PickupTable(final PickupInterface pickupInterface){
		dataProvider = new ListDataProvider<Pickup>();
		table = new CellTable<Pickup>();

		/* Create all the columns */
		
		ClickableTextCell nameCell = new ClickableTextCell();
		Column<Pickup, String> nameColumn = new Column<Pickup, String>(nameCell){
			@Override
			public String getValue(Pickup object) {
				if(object.getVolunteer() == null)
					return "Not Set";
				return object.getVolunteer().getDisplayName();
			}
		};
		
		TextColumn<Pickup> dayColumn = new TextColumn<Pickup>(){
			@Override
			public String getValue(Pickup object) {
				if(object.getOneTimeEventPickup() != null){
					return DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_SHORT)
							.format(object.getOneTimeEventPickup().getStartTime());
				} 
				return object.getRecurringEventPickup().getDaysOfWeek();
			}
		};
		
		ClickableTextCell pickupTimeRange = new ClickableTextCell();
		Column<Pickup, String> pickupTimeRangeColumn = new Column<Pickup, String>(pickupTimeRange){
			@Override
			public String getValue(Pickup object) {
				if(object.getOneTimeEventPickup() != null){
					return object.getOneTimeEventPickup().getTimeRange();
				}
				return object.getRecurringEventPickup().getTimeRange();
			}
		};
		
		ClickableTextCell pickup = new ClickableTextCell();
		Column<Pickup, String> pickupColumn = new Column<Pickup, String>(pickup){
			@Override
			public String getValue(Pickup object) {
				return object.getDonor().getName();
			}
		};
		
		TextColumn<Pickup> foodTypeColumn = new TextColumn<Pickup>(){
			@Override
			public String getValue(Pickup object) {
				return object.getDonor().getFoodType().toString();
			}
		};
		
		ClickableTextCell dropoff = new ClickableTextCell();
		Column<Pickup, String> dropoffColumn = new Column<Pickup, String>(dropoff){
			@Override
			public String getValue(Pickup object) {
				if(object.getRecipient() == null)
					return "Not Set";
				return object.getRecipient().getName();
			}
		};
		
		ClickableTextCell dropOffTimeRange = new ClickableTextCell();
		Column<Pickup, String> dropOffTimeRangeColumn = new Column<Pickup, String>(dropOffTimeRange){
			@Override
			public String getValue(Pickup object) {
				if(object.getRecipient() == null)
					return "";
				if(object.getOneTimeEventDropOff() != null){
					return object.getOneTimeEventDropOff().getTimeRange();
				}
				return object.getRecurringEventDropOff().getTimeRange();
			}
		};
		
		TextColumn<Pickup> notesColumn = new TextColumn<Pickup>(){
			@Override
			public String getValue(Pickup object) {
				return object.getNotes();
			}
		};
		
		TextColumn<Pickup> transType = new TextColumn<Pickup>(){
			@Override
			public String getValue(Pickup object) {
				return object.getTransportationType()+"";
			}
		};
		
		ClickableTextCell editCell = new ClickableTextCell();
		Column<Pickup, String> editColumn = new Column<Pickup, String>(editCell){
			@Override
			public String getValue(Pickup object) {
				return "Edit";
			}
		};
		editColumn.setCellStyleNames(Resources.INSTANCE.ops().textButtonCell());

		/* Add the columns to the table */
		table.addColumn(nameColumn, "Volunteer");
		table.addColumn(pickupColumn, "Pickup");
		table.addColumn(dayColumn, "Day");
		table.addColumn(pickupTimeRangeColumn, "Time Range");
		table.addColumn(foodTypeColumn, "Food Type");
		table.addColumn(dropoffColumn, "Drop Off");
		table.addColumn(dropOffTimeRangeColumn, "Time Range");
		table.addColumn(notesColumn, "Notes");
		table.addColumn(transType, "Bike or Car");
		table.addColumn(editColumn, "Edit");

		dataProvider.addDataDisplay(table);

		ListHandler<Pickup> sortHandler = new ListHandler<Pickup>(dataProvider.getList());
		table.addColumnSortHandler(sortHandler);

		/* Set the columns as sortable and add their comparators to the sort handler */
		nameColumn.setSortable(true);
		sortHandler.setComparator(nameColumn, new Comparator<Pickup>(){
			@Override
			public int compare(Pickup o1, Pickup o2) {
				return o1.getVolunteer().getDisplayName()
						.compareTo(o2.getVolunteer().getDisplayName());
			}
		});
		
		dayColumn.setSortable(true);
		sortHandler.setComparator(dayColumn, new Comparator<Pickup>(){
			@Override
			public int compare(Pickup arg0, Pickup arg1) {
				return 1;//arg0.getDay().compareTo(arg1.getDay());
			}
		});

		pickupTimeRangeColumn.setSortable(true);
		sortHandler.setComparator(pickupTimeRangeColumn, new Comparator<Pickup>(){
			@Override
			public int compare(Pickup o1, Pickup o2) {
				// TODO Auto-generated method stub
				return 0;
			}
		});
		
		pickupColumn.setSortable(true);
		sortHandler.setComparator(pickupColumn, new Comparator<Pickup>(){
			@Override
			public int compare(Pickup o1, Pickup o2) {
				return o1.getDonor().getName().compareTo(o2.getDonor().getName());
			}
		});
		
		foodTypeColumn.setSortable(true);
		sortHandler.setComparator(foodTypeColumn, new Comparator<Pickup>(){
			@Override
			public int compare(Pickup o1, Pickup o2) {
				return o1.getDonor().getFoodType().toString()
						.compareTo(o2.getDonor().getFoodType().toString());
			}
		});
		
		dropoffColumn.setSortable(true);
		sortHandler.setComparator(dropoffColumn, new Comparator<Pickup>(){
			@Override
			public int compare(Pickup o1, Pickup o2) {
				return o1.getRecipient().getName().compareTo(o2.getRecipient().getName());
			}
		});
		
		transType.setSortable(true);
		sortHandler.setComparator(transType, new Comparator<Pickup>(){
			@Override
			public int compare(Pickup o1, Pickup o2) {
				return o1.getTransportationType().toString().compareTo(o2.getTransportationType().toString());
			}
		});
		
		/* Set what happens when the cells are clicked */
		nameColumn.setFieldUpdater(new FieldUpdater<Pickup, String>(){
			@Override
			public void update(int index, Pickup object, String value) {
				Window.alert("This needs to be implemented. You clicked on "+object.getVolunteer().getDisplayName());
			}
		});
		
		editColumn.setFieldUpdater(new FieldUpdater<Pickup, String>(){
			@Override
			public void update(int index, final Pickup object, String value) {
				pickupInterface.onEditPickup(object);
			}
		});

		this.add(table);
		table.setWidth("100%");
		//this.setHeight("100%");
		table.setEmptyTableWidget(new HTML("No schedules are available to view."));
	}
	public void addPickup(Pickup pickup){
		dataProvider.getList().add(pickup);
	}
	public void removePickup(Pickup pickup){
		dataProvider.getList().remove(pickup);
	}
	public void refreshTable()
	{
	  table.redraw();
	}
}
